package com.sky.happygos.common;

import android.animation.ValueAnimator;
import android.content.Context;
import android.support.v4.view.animation.LinearOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.sky.happygos.R;
import com.sky.happygos.utils.DensityUtil;


/**
 * Created by qqqqqqq on 2017/4/16.
 */

public class LoadView extends RelativeLayout {
    ImageView image;
    ValueAnimator animator;

    public LoadView(Context context) {
        super(context);
        init();
    }

    public LoadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LoadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init(){
        this.setVisibility(View.INVISIBLE);
        image = new ImageView(getContext());
        image.setImageResource(R.drawable.app_icon);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.width = DensityUtil.dip2px(getContext(),60);
        layoutParams.height = DensityUtil.dip2px(getContext(),60);
        image.setLayoutParams(layoutParams);

        addView(image);

        animator = ValueAnimator.ofFloat(0,360*100);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Float f = (Float) animation.getAnimatedValue();
                image.setRotation(f);
            }
        });
        animator.setInterpolator(new AnticipateInterpolator());
        animator.setDuration(1300*100);
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.setRepeatCount(99);

    }

    public void start(){
        this.setVisibility(View.VISIBLE);
        animator.start();
    }

    public void dismiss(){
        animator.end();
        this.setVisibility(View.INVISIBLE);
    }




}
